/**
 * 短信后倒计时
 * @param obj: 执行倒计时的对象
 */
function settime(obj) {
    var countdown = 60;

    function daojishi(obj) {
        if (countdown == 0) {
            $(obj).removeAttr("disabled");
            $(obj).html("获取验证码");
            countdown = 60;
            clearInterval(down);
        } else {
            $(obj).attr("disabled", true);
            $(obj).html("重新发送(" + countdown + ")");
            countdown--;
        }
    }

    var down = setInterval(function () {
        daojishi(obj);
    }, 1000)
}

/**
 * 发送短信验证码
 * @param selector1 点击发送短信的按钮
 * @param selector2 手机号所在input
 */
function send_code(selector1, selector2) {
	var me = this;
    var phone = $(selector2).val();
    //验证手机号码是否有效
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if (phone) {
        if (!myreg.test(phone)) {
            myApp_.alert("请输入正确的手机号码！", '提示');
            return 0;
        } else {
            //手机号验证正确
            yanzheng2 = true; 
			var param = {};
			param.valid_success_callback = function(param){
				var  pic_captcha  = param.pic_captcha ;
				var url = "api/V0/phone_send";
			    url = url_(url);
			    var data = {"phone": phone,pic_captcha:pic_captcha};
			    var success = function (data) {
			        if (data.code != 0) {
			            //失败
			            myApp_.alert(data.msg, '提示');
			        } else {
			            //成功
			            myApp_.alert("短信验证码已发送，请查收！", '提示');
			            settime($(selector1));
			        }
			    }
			    //跨域请求
	            request_(url, data, success);
			} ;
			identifying_code(param);
           
        }
    } else {
        myApp_.alert("请您输入手机号码", '提示');
    }
}

/*
 * 发送短信前输入验证码
 */
function identifying_code(param){
	var url = url_('api/V0/pic_captcha');
	myApp_.modal({
	    title: '请输入验证码',
	    afterText:  '<img src="'+url+
	    			'" onclick="this.src=this.src;" height="50" style="display:block;margin:1rem auto;">'+
	                '<input id="identifying_code" type="number" value="" placeholder="请输入验证码" style="padding:5px;" />',
	    buttons: [
	      {
	        text: '取消'
	      },
	      {
	        text: '确定',
	        bold: true,
	        onClick: function () {
	        	var pic_captcha  = $("#identifying_code").val();
	        	if (pic_captcha  == '') {
	        		my_tips_("验证码不能为空！");return;
	        	}
	        	param.pic_captcha  = pic_captcha ;
	        	param.valid_success_callback(param);
	        }
	      },
	    ]
	})
}
 